<template>
  <div class="choice-commidy">
    <div class="already-content">
      <EditTable
        ref="editTable"
        @onChangeSelectAll="onSelectAll"
        @onJoinSale="onJoinSale"
        @onJoinSaleCheck="onJoinSaleCheck"
        @onShowChild="onShowChild"
        @onSearchData="getProductData"
        :listData="columns1"
        :status="status"
        :list="$store.state.fullReturnSale.saleData"
        :isLarge="true"
        :totalItem="totalItem"
      ></EditTable>
    </div>
    <div class="pager">
      <Page 
        :total="totalItem" 
        :current="currentPage"
        :page-size="pageSize"
        @on-change="onPageChange"
        @on-page-size-change="onPageSizeChange"
        show-sizer 
        placement="top">
      </Page>
    </div>
    <Loader :status="status" @refresh="getProductData"></Loader>
  </div>
</template>
<script>
import EditTable from "@/views/sales/common/TableChoiceEven";
import {
 deleteRightTodo,
 deleteRightItem, 
 getCode,
 setArrayValue, 
 setAdd, 
 setExchangeValue,
 onJoinSaleBody,
 compareSaleGift ,
 saveOtherProduct,
 getProductTodo,
 getProductItem
 } from "@/views/sales/common/TableSaleMethods"
import {
  getValueArray,
  setEmptyArray,
  getSelectStatus,
  getValueStatus,
  columns1,
  data1
} from "./ChoiceCommidyMethods";
import { clearEmpty } from '@/util'
export default {
  name: "choicecommidy",
  components: {
    EditTable
  },
  created() {
    if (!this.$store.state.singleSale.saleData.length && !this.$route.query.id) {
      this.getProductData({size: 20, current: 1});
    }
  },
  data() {
    return {
      id: this.$route.query.id || '',
      model1: "",
      value: "",
      cityList: [],
      columns1,
      data1,
      status: "loading",
      totalItem: 0,
      currentPage: 1,
      pageSize: window.$pageSize,
      size: window.$pageSize
    };
  },
  methods: {
    setPage() {
      this.currentPage = 1
      this.pageSize = window.$pageSize
    },
    onPageChange(val) {
      this.currentPage = val
      this.getProductData({size: this.pageSize, current: this.currentPage})
    },
    onPageSizeChange(val) {
      this.pageSize = val
      this.getProductData({size: this.pageSize, current: this.currentPage})
    },
    onShowChild(item) {
      item.showChildren = !item.showChildren;
    },
    getProductData(param) {
      let self = this;
      let params = {
          current: this.currentPage,
          size: this.pageSize,
          ownerCategoryId: this.$store.state.fullReturnSale.searchForm.ownerCategoryId,
          keyword: this.$store.state.fullReturnSale.searchForm.keyword,
          code: this.$store.state.fullReturnSale.searchForm.code,
          skuCode: this.$store.state.fullReturnSale.searchForm.skuCode,
          brandId: this.$store.state.fullReturnSale.searchForm.brandId,
          tireFigure: this.$store.state.fullReturnSale.searchForm.tireFigure,
          tireRim: this.$store.state.fullReturnSale.searchForm.tireRim,
          tireSpec: this.$store.state.fullReturnSale.searchForm.specs
      }
      params = clearEmpty(params)
      self.status = "loading"
      this.$ajaxGet("/activity/product/spu_sku_page", params).then(data => {
          self.$store.state.fullReturnSale.saleData = self.data1 = data.records;
          setAdd(self, self.data1, {index: "checked", val1: false, childName: "productSkuList"});
          setAdd(self, self.data1, {index: "buttonChecked", val1: false, childName: "productSkuList"});
          setAdd(self, self.data1, {index: "showChildren", val1: false, childName: "productSkuList"});
          getCode (self, self.data1, {index: 'parentcode', childName: "productSkuList" });
          // setAdd(self, self.data1, {index: "sale", val1: "",childName: "productSkuList"});
          self.status = "pass";
          this.totalItem = Number(data.total)
          // compareSaleGift(self.$store.state.fullReturnSale.saleData, self.$store.state.fullReturnSale.GetsaleData)
        }).catch(res => {
          self.status = "pass";
        })
    },
    getSaleData(type, item, todo) {
      if (type == 'buttonChecked') {
        if (todo && item.buttonChecked) { // 子集增加
          saveOtherProduct('', {self: this, activityId: this.id, type: type}, getProductTodo(item))
        } else if (!todo && item.buttonChecked) { // 腹肌增加
          saveOtherProduct('', {self: this, activityId: this.id, type: type}, getProductItem(item))
        } else if (todo && !item.buttonChecked) {
          // this.onDeleteSale(item, 'todo')
        } else if (item && !item.buttonChecked) {
          // this.onDeleteSale(item)
        }
      } else {
        this.$store.commit("getFullReturnSaleData", {self: this, activityId: this.id, type: type});
      }
    },
    onDeleteSale(data, type) {
      let ids = []
      if (type == 'todo') {
        ids = [data.id]
      } else {
        ids = data.productSkuList.map(item=> {
          return item.id
        })
      }
      this.status = "loading"
      this.$store.dispatch('deleteSku', {id: ids, activityId: this.id}).then(res => {
        this.status = "pass"
        this.$store.dispatch("getOtherSkuCount", {id: this.id})
        this.$Notice.success({
          title: '温馨提示',
          desc: "删除商品成功"
        })
        // onDeleteSale (index, data, index1, data1, this.$store.state.fullReturnSale.saleData);
        // deleteSale(index, data, index1, data1);
      })
    },
    onSelectAll (param) {
      //点击选中所有checked按钮的时候出发的事件
      this.$store.commit("onFullReturnSelectAll", param)
    },
    onJoinSaleBody(item, todo, params) {
      onJoinSaleBody(item, todo, params)
    },
    onJoinSale(item, todo, selectAll) {
      //点击加入促销的时候触发的事件op
      item.buttonChecked = !item.buttonChecked;
      this.onJoinSaleBody(item, todo, {index:'checked',index1: 'productSkuList' });
      this.onJoinSaleBody(item, todo, {index:'buttonChecked',index1: 'productSkuList' });
      /*
        左侧但是撤出促销的时候--对应右侧的是删除
      */
     if(todo && !todo.buttonChecked || item && !item.buttonChecked){
       if(todo) { //没子集
         deleteRightTodo(item, todo, this.$store.state.fullReturnSale.GetsaleData)
       }else { //有子集
          deleteRightItem(item, this.$store.state.fullReturnSale.GetsaleData)
       }
     }
      this.getSaleData('buttonChecked', item, todo)
    },
    deleteRightTodo (todo1, item1) {
      let that = this;
       this.$store.state.fullReturnSale.GetsaleData.forEach(function(item, index, array){
          if(item1.code == item.code) {
            item.productSkuList.forEach(function(todo, index1, array1){
              if(todo.code == todo1.code) {
                if(item.productSkuList.length == 1){
                  item.productSkuList.splice(index1, 1)
                  that.deleteRightItem(item1)
                }else {
                  item.productSkuList.splice(index1, 1)
                }
              }
            })
          } 
       })
    },
    deleteRightItem (item1) {
      // debugger
      let that = this;
      this.$store.state.fullReturnSale.GetsaleData.forEach(function(item, index, array){
        if(item.code == item1.code){
          that.$store.state.fullReturnSale.GetsaleData.splice(index, 1)
        }
      })
    },
    onJoinSaleCheck(item, todo, selectAll) {
      // 点击checked按钮的时候触发的事件
      this.onJoinSaleBody(item, todo, {index:'checked',index1: 'productSkuList' });
    }
  }
};
</script>

